package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.po.RolePo;
import com.dragee.modules.work.entity.TeamEntity;
import com.dragee.modules.work.entity.TeamOtherEntity;
import com.dragee.modules.work.service.TeamOtherService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 随队人员
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-24 09:40:38
 */
@RestController
@RequestMapping("work/teamother")
@Api(tags = "work - 随队人员")
public class TeamOtherController {
    @Autowired
    private TeamOtherService teamOtherService;
    @Autowired
    private TeamService teamService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("随队人员列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        RolePo role = loginUser.getRole();
        if (role.getRoleKey().equals("duiwu")) {
            // 队伍登录
            TeamEntity team = teamService.getTeamByUserId(loginUser.getUserId().toString());
            params.put("teamKey", team.getId());
        }
        PageUtils page = teamOtherService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("随队人员信息")
    public R info(@PathVariable("id") Long id){
		TeamOtherEntity teamOther = teamOtherService.getById(id);
        return R.ok().put("data", teamOther);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改随队人员")
    public R saveOrUpdate(@RequestBody TeamOtherEntity teamOther){
        TeamEntity team = teamService.getTeamByUserId(LoginUserUtil.getLoginUserId().toString());
        teamOther.setTeamKey(team.getId().toString());
        teamOther.setTeamId(team.getCode());
        teamOtherService.saveOrUpdate(teamOther);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("随队人员删除")
    public R delete( String id){
		teamOtherService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("随队人员批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> teamOtherService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
